import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message, message } from "element-ui"
import  store from "../store"

Vue.use(VueRouter)


// 默认的静态路由
const routes = [
  {
    path: '/login',
    name: 'login',
    meta: {
      title: "登陆",
      roles: ["admin", "teacher", "student"]
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/user/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      title: "注册",
      roles: ["admin", "teacher", "student"]
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/user/Register.vue')
  },
  {
    path: '/findPassword',
    name: 'findPassword',
    meta: {
      title: "注册",
      roles: ["admin", "teacher", "student"]
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/user/findPassword.vue')
  },

]

// 动态路由
export const asyncRouter = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/home/Home.vue'),
    redirect: "/workform",
    children: [
      {
        path: 'classJur',     // 对老师，学生的权限设置
        name: 'classJur',
        meta: {
          title: "分类权限管理",
          roles: ["admin"]
        },
        component: () => import(/* webpackChunkName: "jurisdiction" */ '../views/home/jurisdiction/classJur.vue')
      },
      {
        path: 'userJur',     // 对单独地用户的权限设置
        name: 'userJur',
        meta: {
          title: "用户权限管理",
          roles: ["admin"]
        },
        component: () => import(/* webpackChunkName: "jurisdiction" */ '../views/home/jurisdiction/userJur.vue')
      },
      {
        path: 'workform',     // 表格
        name: 'workform',
        meta: {
          title: "升学就业表格",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "formList" */ '../views/home/formList/workForm.vue')
      },
      {
        path: 'workEcharts',    // echarts可视化
        name: 'workEcharts',
        meta: {
          title: "升学就业可视化",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/workEcharts.vue')
      },
      {
        path: 'forum',  // 论坛
        name: 'forum',
        meta: {
          title: "学生论坛",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "forum" */ '../views/home/forum/forum.vue')
      },
      {
        path: 'account',  // 账号管理，个人信息修改中心
        name: 'account',
        meta: {
          title: "账号信息管理",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "Administration" */ '../views/home/Administration/account.vue')
      },

      {
        path: 'authentication',  // 老师资格认证模块
        name: 'authentication',
        meta: {
          title: "教师资格认证",
          roles: ["admin"]
        },
        component: () => import(/* webpackChunkName: "Administration" */ '../views/home/Administration/authentication.vue')
      },

      {
        path: 'upload',  // 就业信息上传
        name: 'upload',
        meta: {
          title: "就业信息上传",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "Administration" */ '../views/home/Administration/upload.vue')
      },
      {
        path: 'employment',  // 五年毕业学历结构
        name: 'employment',
        meta: {
          title: "学历结构",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/employment.vue')
      },
      {
        path: 'sexstract',  // 五年毕业学历结构(依据性别筛选)
        name: 'sexstract',
        meta: {
          title: "性别占比",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/sexstract.vue')
      },
      {
        path: 'map',  // 毕业生就业分布
        name: 'map',
        meta: {
          title: "就业分布",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/map.vue')
      },
      {
        path: 'college',  // 毕业生学院详情
        name: 'college',
        meta: {
          title: "学院详情",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/college')
      },
      {
        path: 'major',  // 毕业生专业详情
        name: 'major',
        meta: {
          title: "学院详情",
          roles: ["admin", "teacher", "student"]
        },
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/echarts/major')
      },

    ]
  },
]

// 静态路由  处理错误路由
const endRouter = [
  {
    path: '*',
    name: 'err',
    meta: {
      title: "404",
      roles: ["admin", "teacher", "student"]
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/common/404.vue')
  }
]

// 导出一个专门处理动态路由和映射的函数
// 需要参数 当前从后端获取的路由权限列表
export function handleRouter(routerlist) {
  // console.log(routerlist[0].children.length)

  for (let i = 0; i < asyncRouter.length; i++) {
    for (let j = 0; j < routerlist.length; j++) {
      // 二级路由
      if (asyncRouter[i].name == routerlist[j].name && routerlist[j].children && routerlist[j].children.length > 0) {
        for (let k = 0; k < asyncRouter[i].children.length; k++) {
          for (let l = 0; l < routerlist[j].children.length; l++) {
            // 子路由
            if (asyncRouter[i].children[k].name == routerlist[j].children[l].name) {
              routerlist[j].children[l].component = asyncRouter[i].children[k].component

            }
          }
        }
      }
      // 一级路由
      if (asyncRouter[i].name == routerlist[j].name) {
        routerlist[j].component = asyncRouter[i].component
      }
    }
  }

  // 拼接收尾路由
  routerlist = [...routerlist, ...endRouter]
  // console.log(routerlist)
  router.addRoutes(routerlist); // 一旦刷新这儿添加的动态路由就会消失
}


const router = new VueRouter({
  routes
})


//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  let arr = ["/login", "/register","/findPassword"]   // 不需要token的列表
  let index = arr.findIndex(item => {
    return item == to.path
  })

  if (index !== -1) {
    document.title = to.meta.title;
    return next()
  } else {
    //获取在登陆时返回数据里面保存在内存中的token值
    let token = sessionStorage.getItem("token")

    if (!token) {
      message.error("没有登陆，请先登陆")
      document.title = to.meta.title;
      return next("/login")
    } else {
      // 进入了需要token的页面，并已经成功从后端获取到了token
      document.title = to.meta.title;
      next()
    }
  }
})


export default router
